package com.camel.weixin.controller;


import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.camel.core.controller.BaseCommonController;
import com.camel.core.entity.Result;
import com.camel.core.enums.YesNo;
import com.camel.core.service.BaseCommonService;
import com.camel.core.utils.HttpUtils;
import com.camel.core.utils.ResultUtil;
import com.camel.weixin.model.WxAccount;
import com.camel.weixin.model.WxMenu;
import com.camel.weixin.service.WxAccountService;
import com.camel.weixin.service.WxMenuService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/wxMenu")
public class WxMenuController extends BaseCommonController<WxMenu> {

    @Resource
    private WxMenuService<WxMenu> service;

    @Resource
    private WxAccountService<WxAccount> wxAccountService;

    @Override
    public BaseCommonService<WxMenu> getiService() {
        return service;
    }

    @PostMapping("/deploy")
    public Result deploy(@RequestBody WxMenu entity) {
        WxMenu byId = service.getOne(new QueryWrapper<WxMenu>().eq("wx_account_id", entity.getWxAccountId()), false);

        WxAccount wxAccount = wxAccountService.getById(entity.getWxAccountId());

        if(wxAccount.getAuthentication().equals(YesNo.NO)) {
            return ResultUtil.error("公众号未认证，无法获取完整api权限，请确认公众号状态或认证后再次尝试！");
        }
        if (System.currentTimeMillis() - wxAccount.getTokenTime().getTime() > 7100 * 1000) {
            wxAccountService.refreshToken(entity.getWxAccountId());
            wxAccount = wxAccountService.getById(entity.getWxAccountId());
        }

        JSONObject json = HttpUtils.postWx(JSONUtil.parseObj(byId.getMenuJson()), "https://api.weixin.qq.com/cgi-bin/menu/create", wxAccount.getAccessToken());
        if(!ObjectUtils.isEmpty(json) && json.getInt("errcode").equals(48001)) {
            return ResultUtil.error("api 功能未授权，请确认公众号已获得该接口，可以在公众平台官网 - 开发者中心页中查看接口权限");
        }
        return ResultUtil.success(json);
    }

    @Override
    public Result save(@RequestBody WxMenu entity) {
        WxMenu wxMenu = service.getOne(new QueryWrapper<WxMenu>().eq("wx_account_id", entity.getWxAccountId()));
        if (!ObjectUtils.isEmpty(wxMenu)) {
            wxMenu.setMenuJson(entity.getMenuJson());
            return super.update(wxMenu);
        }
        entity.setIsPublish(YesNo.NO.getValue());
        return super.save(entity);
    }

    @PostMapping("/menus")
    public Result menus(@RequestBody WxMenu wxMenu) {
        return ResultUtil.success(service.getOne(new QueryWrapper<WxMenu>().eq("wx_account_id", wxMenu.getWxAccountId())));
    }

    @Override
    public String getMouduleName() {
        return "微信公众号菜单";
    }
}
